package com.itheima.exception;

import com.itheima.pojo.Result;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionAdvice {

    @ExceptionHandler(value= Exception.class)
    public Result getExceptionInfo(Exception e){
        e.printStackTrace();
        return Result.error("服务器异常");
    }

    @ExceptionHandler(value= TliasException.class)
    public Result getTliasException(Exception e){
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(value= DuplicateKeyException.class)
    public Result getDuplicatekeyExceptionInfo(DuplicateKeyException duplicateKeyException) {

        //     1打印信息
        duplicateKeyException.printStackTrace();

        //     1,获取异常信息
        String message = duplicateKeyException.getMessage();

        if (message.contains("emp.username")) {
            return Result.error("用户名已经存在");
        }
        if (message.contains("emp.phone")) {
            return Result.error("手机号已经存在");
        }

        return Result.error("数据已经存在");

    }
}
